package com.qingyun.user.controller;



import cn.hutool.core.util.ObjectUtil;
import com.qingyun.model.common.dtos.R;
import com.qingyun.model.common.enums.AppHttpCodeEnum;
import com.qingyun.model.user.dtos.LoginFormByPasswordDTO;
import com.qingyun.model.user.dtos.RegDto;
import com.qingyun.model.user.pojos.User;
import com.qingyun.user.service.UserFollowService;
import com.qingyun.user.service.UserService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;
import utils.thread.AppThreadLocalUtil;

import javax.annotation.Resource;

@Api(tags = "管理用户接口" )
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private UserFollowService userFollowService;
    /**
     * 登出功能
     * @return 无
     */
    @PostMapping("/logout")
    public R logout(){
        AppThreadLocalUtil.clear();
        return R.success(AppHttpCodeEnum.SUCCESS);
    }


    @GetMapping("/me")
    public R<User> me(){
        User user = AppThreadLocalUtil.getUser();
        if (ObjectUtil.isEmpty(user))return R.error("登录过期！请重新登录！");
        if (user.getId()==null)return R.error("登录过期！请重新登录！");
        Integer follow = userFollowService.query().eq("user_id", user.getId()).count();
        user.setFollowCount(follow);
        Integer fans = userFollowService.query().eq("follow_id", user.getId()).count();
        user.setFansCount(fans);
        return R.success(user);
    }
    /**
     * 登录功能
     * @param loginForm
     */
    @PostMapping("/loginByPassword")
    public R loginByPassword(@RequestBody LoginFormByPasswordDTO loginForm){
        return userService.loginByPassword(loginForm);
    }


    @PostMapping("/reg")
    public R reg(@RequestBody RegDto dto) {
        return userService.reg(dto);
    }


    /**
     * 信息
     */
    @GetMapping ("/info/{id}")
    public R<User> info(@PathVariable("id") Long id){

        return userService.info(id);
    }

}